#Twisted Shootout
#Copyright (C) 2007  Han Dao
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

#You can contact the author at wikipediankiba@gmail.com
import pygame

class Collision_Detector:
	def __init__(self,w,e,p,sprites):
		self.w = w
		self.e = e
		self.p = p
		self.sprites = sprites
	def add(self,m,i):
		self.m = m
		self.i = i
	def detect(self):
		n = 0
		for e in self.e.e:
			if self.collide(e.rect) == True:
				e.health = e.health - 1
				if e.health < 1:
					self.sprites.remove(self.e.e[n])
					self.e.e.pop(n)
			n = n + 1
		if self.collide(self.p.rect) == True:
			self.p.health = self.p.health - 1
	def collide(self,e):
		n = 0
		for w in self.w.w:
			if w.rect.colliderect(e):
				self.sprites.remove(self.w.w[n])
				self.w.w.pop(n )
				return True
			n = n + 1
		return False
	def map_collide(self,t):
		for m in self.m:
			if m.property == True:
				if m.rect.colliderect(t.rect):
					x = m.rect.x
					y = m.rect.y
					return 1
		return 2
	def unitcollide(self,e):
		if self.p.rect.colliderect(e):
			return True
	def knifecombat(self):
		if self.p.melee == True:
			n = 0
			for e in self.e.e:
				if self.unitcollide(e.rect) == True:
					self.sprites.remove(self.e.e[n])
					self.e.e.pop(n)
				n = n + 1
	def bulletobj(self):
		for m in self.m:
			if m.property == True:
				self.collide(m.rect)
	def itemsobj(self):
		n = 0
		for i in self.i.items:
			if self.p.rect.colliderect(i):
				if i.kind == 0:
					if self.w.ammo > 0:
						self.w.ammo = self.w.ammo - 30
						if self.w.ammo < 0:
							self.w.ammo = 0
						return n
				if i.kind == 1:
					if self.p.health != 30:
						self.p.health = 30
						return n
			n = n + 1
		return -1
	def wincollide(self):
		for m in self.m:
			if m.win == True:
				if m.rect.colliderect(self.p.rect):
					if self.p.rect.y > m.rect.y:
						if self.p.rect.x >= m.rect.x:
							self.p.win = True